# -*- coding: utf-8 -*-
# @file    : util.py
# @Date    : 2020/10/31
# @Author  :
# @Version : 1.0.0
import win32clipboard as w
import win32con
import os
import sys


def get_clipboard_text():
    w.OpenClipboard()
    d = w.GetClipboardData(win32con.CF_TEXT)
    w.CloseClipboard()
    return d.decode('GBK')


def load_argv(argv):
    """
    simple argv loader
    :param argv:
    :return:
    """
    commands = []
    options = []
    flag = False
    while len(argv):
        part = argv.pop(0)
        if part.startswith("--"):
            option, *value = part.split('=', maxsplit=1)
            options.append([option, "".join(value)])
            flag = False

        elif part.startswith("-"):
            if flag:
                options[-1].append("")
            options.append([part])
            flag = True

        else:
            if not flag:
                commands.append(part)
            else:
                options[-1].append(part)
                flag = False

    if flag is True:
        options[-1].append("")
    options = dict(options)
    return commands, options


_, options = load_argv(sys.argv[1:])


# copy from flask framework
def get_args_for_reloading():
    """Determine how the script was executed, and return the args needed
    to execute it again in a new process.
    """
    rv = [sys.executable]
    py_script = sys.argv[0]
    args = sys.argv[1:]
    # Need to look at main module to determine how it was executed.
    __main__ = sys.modules["__main__"]

    # The value of __package__ indicates how Python was called. It may
    # not exist if a setuptools script is installed as an egg. It may be
    # set incorrectly for entry points created with pip on Windows.
    if getattr(__main__, "__package__", None) is None or (
            os.name == "nt"
            and __main__.__package__ == ""
            and not os.path.exists(py_script)
            and os.path.exists(py_script + ".exe")
    ):
        # Executed a file, like "python app.py".
        py_script = os.path.abspath(py_script)

        if os.name == "nt":
            # Windows entry points have ".exe" extension and should be
            # called directly.
            if not os.path.exists(py_script) and os.path.exists(py_script + ".exe"):
                py_script += ".exe"

            if (
                    os.path.splitext(sys.executable)[1] == ".exe"
                    and os.path.splitext(py_script)[1] == ".exe"
            ):
                rv.pop(0)

        rv.append(py_script)

    rv.extend(args)
    return rv


if __name__ == '__main__':
    print(get_clipboard_text())
